package com.anysoft.base;

import android.content.Context;

import com.anysoft.widget.LoadingDialog;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * Created by LiTingYao on 2018/5/2.
 */

public abstract class BasePresenter<V> {
    
    protected Reference<V> mViewRef;
    
    
    public void attachView(V view) {
        mViewRef = new WeakReference<V>(view);
    }
    
    protected V getView() {
        return mViewRef.get();
    }
    
    public boolean isViewAttached() {
        return mViewRef != null && mViewRef.get() != null;
    }
    
    public void detachView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
    }
    
    private LoadingDialog loadingDialog;
    
    public void showLoading(Context context) {
        if (loadingDialog == null && !((BaseBaseActivity) context).isFinishing()) {
            loadingDialog = new LoadingDialog(context);
            loadingDialog.setCancelable(false);
            loadingDialog.setAnimationEnable(true);
            loadingDialog.show();
        }
    }
    
    public void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }
}
